import { debounce } from '@/utils/auth'
import pub from '@/utils/pub'

/**
 * 使用proxyGoods类后无需关注修改数量价格后数量计算问题 减少代码量
 * @author jjw
 */
class ProxyGoods {}
ProxyGoods.prototype.changeCount = function(pop) {
  const _boxUnitCount = Number(this.boxUnitCount) >> 0
  if (!_boxUnitCount) throw Error('未配置换算率')
  if (pop === 'goodsCount') {
    this.boxNum = (this.goodsCount / _boxUnitCount) >> 0
    this.mainNum = this.goodsCount % _boxUnitCount
  } else {
    this[pop] = Number(this[pop])
    // 改变零数计算数量
    let mainNum = 0
    let boxNum = 0
    // 判断当前零数是否大于箱单位计量数
    if (this.mainNum >= this.boxUnitCount) {
      mainNum = this.mainNum % _boxUnitCount
      boxNum = (this.mainNum / _boxUnitCount) >> 0
      this.boxNum = boxNum + this.boxNum
      this.mainNum = mainNum
    }
    this.taxBoxAmount = this.boxNum * this.taxBoxPrice
    this.taxMainAmount = this.mainNum * this.taxPrice
    this.goodsCount = this.boxNum * this.boxUnitCount + this.mainNum
    this.noTaxAmount = Number(this.noTaxPrice) * Number(this.goodsCount || 0).toFixed(6)
  }
  this.taxAmount = this.goodsCount * this.taxPrice

  this.strategy && this.strategy()
  // 数据更新
  pub.$pub('updateGoodsData', this)
  // 如果该商品是满赠则发布订阅 通知ShoppingCardList进行更新
  if (this.promotionType === '3') {
    pub.$pub('changePromotion', this)
  }
}
ProxyGoods.prototype.changeAmount = function(pop) {
  // 暂时不做促销
  // 金额算单价
  if (pop === 'taxAmount') {
    this.taxPrice = (Number(this.taxAmount || 0) / Number(this.goodsCount || 0)).toFixed(6)
    this.taxBoxPrice = Number(this.taxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
    this.noTaxPrice = Number(this.taxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
    this.noTaxBoxPrice = Number(this.taxBoxPrice || 0) / (1 + Number(this.taxRate || 0) / 100).toFixed(6)
    this.noTaxAmount = Number(this.noTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
    this.tax = Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0).toFixed(6)
    this.billTaxPrice = this.taxPrice
    this.billTaxBoxPrice = this.taxBoxPrice
  } else if (pop === 'taxBoxPrice') {
    this.taxPrice = (Number(this.taxBoxPrice) / (Number(this.boxUnitCount))).toFixed(6)
    this.noTaxPrice = (Number(this.taxPrice) / (1 + Number(this.taxRate) / 100)).toFixed(6)
    this.noTaxBoxPrice = (Number(this.taxBoxPrice) / (1 + Number(this.taxRate) / 100)).toFixed(6)
    this.noTaxAmount = Number(this.noTaxPrice * this.goodsCount).toFixed(6)
    this.taxAmount = (Number(this.taxPrice || 0) * Number(this.goodsCount || 0)).toFixed(6)
    this.tax = (Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0)).toFixed(6)
  } else {
    this.taxBoxPrice = Number(this.taxPrice || 0) * Number(this.boxUnitCount || 0).toFixed(6)
    this.noTaxPrice = Number(this.taxPrice) / (1 + Number(this.taxRate) / 100).toFixed(6)
    this.noTaxBoxPrice = Number(this.taxBoxPrice) / (1 + Number(this.taxRate) / 100).toFixed(6)
    this.taxAmount = Number(this.taxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
    this.noTaxAmount = Number(this.noTaxPrice || 0) * Number(this.goodsCount || 0).toFixed(6)
    this.tax = Number(this.taxAmount || 0) - Number(this.noTaxAmount || 0).toFixed(6)
  }
  this.billTaxPrice = this.taxPrice
  this.billTaxBoxPrice = this.taxBoxPrice
  this.billTaxAmount = Number(this.billTaxPrice) * Number(this.goodsCount)
  this.strategy && this.strategy()
  // 价格变动不触发订阅
  // 数据更新
  pub.$pub('updateGoodsData', this)
}
// 变更需要计算数量的属性
const countType = ['mainNum', 'boxNum', 'goodsCount']
// 变更需要计算金额的属性
const amountType = ['taxPrice', 'taxBoxPrice', 'taxAmount']

const proxyGoods = function(goods, strategy) {
  const curGoods = Object.assign(new ProxyGoods(), goods)
  const proxyGoods = new Proxy(curGoods, {

    set(target, p, newValue, receiver) {
      const isUp = Reflect.set(target, p, newValue, receiver)
      if (isUp) {
        if (countType.includes(p)) {
          debounce(function() {
            target.changeCount(p)
          },)()
        } else if (amountType.includes(p)) {
          target.changeAmount(p)
        }
      }
      return isUp
    }
  })
  if (strategy) {
    proxyGoods.strategy = strategy
  }
  return proxyGoods
}
export default proxyGoods
